<% @view_title = 'Stage Detail' -%>

<% @show_stage_status_bar = true -%>
<div class="sub_tabs_container">
    <ul>
        <%= link_with_current_tab 'Overview', "overview" %>
        <%= link_with_current_tab 'Materials', "materials" %>
        <%= link_with_current_tab 'Jobs', "jobs" %>
        <%= link_with_current_tab 'Config', "stage_config" %>
        <%= link_with_current_tab 'Graphs', "stats" %>
    </ul>
</div>

<div id="tab_container">
    <% case params[:action]
        when 'overview' %>
        <%= render :partial=> 'overview' %>
    <% when 'jobs' %>
        <div id="jobs_grid">
            <%= render :partial=> 'jobs', :locals => {:scope => {:jobs => @jobs, :stage => @stage, :has_operate_permissions => @has_operate_permissions }} %>
        </div>
    <% when 'materials' %>
        <div class="material_tab">
            <%= render :partial => 'shared/build_cause', :locals => {:scope => {:overview => false, :material_revisions => @pipeline.getCurrentRevisions(), :show_files => true, :pipeline_name => @pipeline.getName()}} -%>
        </div>
    <% when 'stats' %>
        <div id="stage_stats">
          <div class="stats">
            <div id="chart_details_container">
              <iframe id="chart_details_iframe" sandbox="allow-scripts" width="100%" height="400px" src="<%= stage_detail_tab_stats_iframe_path() %>"></iframe>
            </div>
          </div>
        </div>
        <script>
            window.addEventListener("message", function(event) {
              var chartIframe = document.getElementById("chart_details_iframe");
              if (event.source === chartIframe.contentWindow) {
                    var data = JSON.parse(event.data);
                    if (data.inFrame) {
                        chartIframe.src = data.openLink;
                    } else {
                        window.location.href = data.openLink;
                    }
                }
            }, false);
        </script>
    <% when 'stage_config' %>
        <div id="ran_with_config">
            <%= render :partial => "config", :locals => {:scope => {:config_revision => @ran_with_config_revision}} -%>
        </div>
    <% end %>
</div>

<script type="text/javascript">

    $(function() {
      var stage_run_timestamp_format_handler = function() {
        $(".schedule_info .time").each(function (idx, timeSpan) {
          var timestamp = parseInt($(timeSpan).attr("data"));
          if (isNaN(timestamp)) return;
          var time = new Date(timestamp);
          $(timeSpan).text(moment(time).format('DD MMM YYYY [at] HH:mm:ss [Local Time]'));
        });
      };

      stage_run_timestamp_format_handler()

      <% unless ['stats'].include?(params[:action]) -%>
          AjaxRefreshers.addRefresher(new StageDetailAjaxRefresher('<%= stage_detail_tab_path_for(:format => "json", :action => params[:action]) %>', {current_stage_run: wire_up_stage_runs_popup, stage_history: compare_link_handlers, stage_run_details: stage_run_timestamp_format_handler}), true);
      <% end %>
  });
</script>
